package bb.lanxing.mvp.model;

import android.util.JsonReader;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import bb.lanxing.model.LevelDataItem;
import bb.lanxing.mvp.model.i.LevelDataModel;
//import bb.lanxing.network.BiciHttpClient;
import bb.lanxing.network.MyHttpClient;
import okhttp3.Response;
import rx.Observable;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class AreaLevelDataModelImpl implements LevelDataModel {
    private List<LevelDataItem> rootItem;

    @Override
    public Observable<List<LevelDataItem>> requestLevelData(final Long parentId, final Long level) {
        if (parentId == null) {
            return requestLevelData();
        }
        return requestLevelData().flatMap((Func1<List<LevelDataItem>, Observable<List<LevelDataItem>>>) levelDataItems -> {
            if (levelDataItems != null) {
                for (LevelDataItem levelDataItem : levelDataItems) {
                    if (levelDataItem.getId() != null && levelDataItem.getId().equals(parentId)) {
                        return Observable.just(levelDataItem.getChildren());
                    }
                }
            }
            return Observable.just(Collections.emptyList());
        });
    }

    public Observable<List<LevelDataItem>> requestLevelData() {
        List<LevelDataItem> list = this.rootItem;
        if (list != null) {
            return Observable.just(list);
        }
        return Observable.create((Observable.OnSubscribe<List<LevelDataItem>>) subscriber -> {
            try {
                List<LevelDataItem> requestAreaFromServer = requestAreaFromServer();
                AreaLevelDataModelImpl.this.rootItem = new ArrayList<>(requestAreaFromServer);
                subscriber.onNext(requestAreaFromServer);
                subscriber.onCompleted();
            } catch (IOException e) {
                subscriber.onError(e);
            }
        }).subscribeOn(Schedulers.io());
    }

    @Override
    public Observable<LevelDataItem> requestLevelData(final Long id) {
        return requestLevelData().flatMap((Func1<List<LevelDataItem>, Observable<LevelDataItem>>) levelDataItems -> Observable.just(AreaLevelDataModelImpl.this.findItemById(id, levelDataItems)));
    }

    public LevelDataItem findItemById(Long id, List<LevelDataItem> levelDataItems) {
        LevelDataItem levelDataItem = null;
        if (levelDataItems != null) {
            for (LevelDataItem levelDataItem2 : levelDataItems) {
                if (levelDataItem2.getId() != null && levelDataItem2.getId().equals(id)) {
                    return levelDataItem2;
                }
                levelDataItem = findItemById(id, levelDataItem2.getChildren());
                if (levelDataItem != null) {
                    break;
                }
            }
        }
        return levelDataItem;
    }

    public List<LevelDataItem> requestAreaFromServer() throws IOException {
        Response execute = MyHttpClient.requestAreaData().execute();
        if (execute.isSuccessful()) {
            return parseArea(execute.body().string());
        }
        return Collections.emptyList();
    }

    private List<LevelDataItem> parseArea(String json) throws IOException {
        String str;
        String str2 = null;
        JsonReader jsonReader;
        long j = 0;
        str = json;
        try {
            str2 = new JSONObject(str).getJSONObject("area").toString();
        } catch (JSONException unused) {
            str2 = str;
            jsonReader = new JsonReader(new StringReader(str2));
            ArrayList arrayList = new ArrayList();
            jsonReader.beginObject();
            long j2 = 1;
            while (jsonReader.hasNext()) {
            }
            jsonReader.endObject();
            return arrayList;
        }
        jsonReader = new JsonReader(new StringReader(str2));
        ArrayList arrayList2 = new ArrayList();
        jsonReader.beginObject();
        long j22 = 1;
        while (jsonReader.hasNext()) {
            String nextName = jsonReader.nextName();
            long j3 = j22 + 1;
            ArrayList arrayList3 = new ArrayList();
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                j = j3;
                if (jsonReader.hasNext()) {
                    String nextName2 = jsonReader.nextName();
                    j3 = j + 1;
                    ArrayList arrayList4 = new ArrayList();
                    jsonReader.beginArray();
                    while (jsonReader.hasNext()) {
                        arrayList4.add(new AreaLevelData(j3, j, 2L, jsonReader.nextString(), null));
                        j3++;
                    }
                    jsonReader.endArray();
                    arrayList3.add(new AreaLevelData(j, j22, 1L, nextName2, arrayList4));
                }
            }

            jsonReader.endObject();
            arrayList2.add(new AreaLevelData(j22, null, 0L, nextName, arrayList3));
            j22 = j;
        }
        jsonReader.endObject();
        return arrayList2;
    }


    public static class AreaLevelData implements LevelDataItem {
        private List<LevelDataItem> children;

        private Long id;
        private Long level;
        private Long parentId;
        private String title;

        public AreaLevelData(Long id, Long parentId, Long level, String title, List<LevelDataItem> children) {
            this.id = id;
            this.parentId = parentId;
            this.level = level;
            this.title = title;
            this.children = children;
        }

        @Override
        public Long getId() {
            return this.id;
        }

        @Override
        public Long getParentId() {
            return this.parentId;
        }

        @Override
        public Long getLevel() {
            return this.level;
        }

        @Override
        public String getTitle() {
            return this.title;
        }

        @Override
        public List<LevelDataItem> getChildren() {
            return this.children;
        }
    }
}
